package org.adream.account.util;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 非静态类 不含静态方法 调用请注入
 * @author Administrator
 *
 */
@Component
public class SolrUtils {
	
	@Value("${solr.baseurl}")
	private String solrBaseUrl;   //纯 URL
	 
	@Value("${solr.core}")
	private String solrCore;
	
	@Value("${solr.schoolCore}")
	private String schoolCore;
	
	@Value("${solr.schoolBasicCore}")
	private String schoolBasicCore;
	
	@Value("${solr.user}")
	private String solrUser;
	
	@Value("${solr.pwd}")
	private String solrPwd;

	/**
	 * 	获取SolrClient solrj 6.5及以后版本获取方式
	 */
	public HttpSolrClient getSolrClient() {
		return new HttpSolrClient.Builder(solrBaseUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
	}
	public QueryResponse process(SolrParams solrParams,String collection) throws SolrServerException, IOException {
		QueryRequest queryRequest = new QueryRequest(solrParams);
		queryRequest.setBasicAuthCredentials(solrUser, solrPwd);
		return queryRequest.process(getSolrClient(),collection);
	}
	
	

	public SolrDocumentList query(String queryString) throws SolrServerException, IOException {
		queryString = "title:" + queryString + "^9 OR tags:" + queryString + "^5 content:" + queryString + "^3 author:"
				+ queryString + "^6";
		// 定义查询条件
		Map<String, String> params = new HashMap<String, String>();
		params.put("q", queryString);
		params.put("start", "0");
		params.put("rows", "20");

		SolrParams solrParams = new MapSolrParams(params);
		QueryResponse queryResponse = process(solrParams, solrCore);
		// 获取结果集
		SolrDocumentList results = queryResponse.getResults();

		return results;
	}
	public SolrDocumentList querySchool(String queryString,String other) throws SolrServerException, IOException {
		queryString = "title:" + queryString ;
		if (!StringUtils.isEmpty(other)) {
			queryString = queryString + other;
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("q", queryString);
		//需要分页
		params.put("start", "0");
		params.put("rows", "20");

		SolrParams solrParams = new MapSolrParams(params);
	 
		QueryResponse queryResponse = process(solrParams, schoolCore);
		// 获取结果集
		SolrDocumentList results = queryResponse.getResults();

		return results;
	}
	public QueryResponse highlightSearch(String queryString,String source,String time,String sort,Integer pageNo,Integer pageSize) throws SolrServerException, IOException {
		queryString = "title:" + queryString + "^9 OR author:"+ queryString +"^6 OR tags:" + queryString + "^5 OR content:" + queryString + "^3";
		if (!StringUtils.isEmpty(source)) {
			queryString = "("+queryString+") AND source:"+source;
		}
		if (!StringUtils.isEmpty(time)) {
		 
			try {
				long changeTime = 0L;
				long oneDayTime = 86400000L;
				long nowTime = new Date().getTime();
				switch (time.trim()) {
				case "oneWeek":
					 
					changeTime = nowTime-(oneDayTime*7);
					break;
				case "oneMonth":
					 
					changeTime = nowTime-(oneDayTime*30);
					break;
				case "oneYear":
					 
					changeTime = nowTime-(oneDayTime*365);
					break;
				default:
				 
					break;
				}
				if(changeTime!=0L) {
					queryString = "("+queryString+") AND time:["+changeTime+" TO "+nowTime+"]";
				}
			} catch (Exception e) {
			 
			}
		}
		pageNo = pageNo == null?Constant.ZERO:(pageNo-1);
		pageSize = pageSize == null?Constant.TEN:pageSize;
		
		pageNo = pageNo * pageSize;
		
		// 定义查询条件
		Map<String, String> params = new HashMap<String, String>();
		params.put("q",queryString);
		//时间排序
		if(!StringUtils.isEmpty(sort)&&("asc".equals(sort)||"desc".equals(sort))) {
			params.put("sort", "time "+sort);
		}
//		params.put("fl", "id,content,title,author,tags");
		params.put("hl.fl", "title,content,author,tags");
		params.put("hl.simple.pre", "<font color=\"red\">");
		params.put("hl.simple.post", "</font>");
		params.put("hl", "on");
		params.put("start", pageNo.toString());
		params.put("rows", pageSize.toString());
		 
		SolrParams solrParams = new MapSolrParams(params);
		QueryResponse queryResponse = process(solrParams,solrCore);
		return queryResponse;
	}
	public SolrDocumentList schoolBasicSearch(String queryString,String other) throws SolrServerException, IOException {
		queryString = "title:" + queryString ;
		if (!StringUtils.isEmpty(other)) {
			queryString = queryString + other;
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("q", queryString);
		//需要分页
		params.put("start", "0");
		params.put("rows", "20");

		SolrParams solrParams = new MapSolrParams(params);
	 
		QueryResponse queryResponse = process(solrParams, schoolBasicCore);
		// 获取结果集
		SolrDocumentList results = queryResponse.getResults();

		return results;
	}
}